import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bilibili/core/viewmodel/live_room_view_model.dart';
import 'package:flutter_bilibili/core/viewmodel/upload_video_notifier.dart';
import 'package:flutter_bilibili/core/viewmodel/video_view_model.dart';
import 'package:flutter_bilibili/ui/pages/publish/take_photos.dart';
import 'package:flutter_bilibili/ui/shared/app_theme.dart';
import 'package:flutter_bilibili/ui/shared/size_fit.dart';
import 'package:flutter_bilibili/core/viewmodel/player_notifier.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:jpush_flutter/jpush_flutter.dart';
import 'package:provider/provider.dart';

import 'core/router/router.dart';
import 'core/viewmodel/base_data_view_model.dart';

main() async {
  ///缓存视频导入和初始化
  WidgetsFlutterBinding.ensureInitialized();
  await FlutterDownloader.initialize(debug: debug, ignoreSsl: true);
  runApp(
    MultiProvider(
      ///配置共享数据
      providers: [
        ///基础数据包括主页每次加载多少数据，加载多少页，分区是什么
        ChangeNotifierProvider(create: (ctx) => HYBaseDataViewModel()),

        ///直播间贡共享数据，包括当前有哪些直播间
        ChangeNotifierProvider(create: (ctx) => HYLiveRoomViewModel()),

        ///发布视频
        ChangeNotifierProvider(create: (ctx) => PlayerNotifier.init()),
        ChangeNotifierProvider(create: (ctx) => UploadVideoNotifier()),

        ///代理，当HYVideoViewModel需要用到HYBaseDataViewModel的数据时，使用代理。视频详细数据
        ChangeNotifierProxyProvider<HYBaseDataViewModel, HYVideoViewModel>(
          create: (cts) => HYVideoViewModel(),
          update: (ctx, baseDataVM, videoVM) {
            videoVM?.updateBaseData(baseDataVM);
            return videoVM as HYVideoViewModel;
          },
        )
      ],
      child: MyApp(),
    ),
  );
}

class MyApp extends StatelessWidget {
  ///APP从此处开始
  @override
  Widget build(BuildContext context) {
    ///初始化屏幕适配
    HYSizeFit.initialize();

    return MaterialApp(
      ///去掉右上角的debug
      debugShowCheckedModeBanner: false,

      ///APP名称
      title: '哔哩哔哩',

      ///主题
      theme: HYAppTheme.norTheme,

      ///起始路由
      initialRoute: HYRouter.initialRoute,

      ///对应的路由
      routes: HYRouter.routes,

      ///后面再改
      onGenerateRoute: HYRouter.generateRoute,

      ///未找到也面跳转至该页
      onUnknownRoute: HYRouter.unKnowRoute,

      ///smartDialog 插件需要初始化
      navigatorObservers: [FlutterSmartDialog.observer],

      ///smartDialog 插件需要初始化
      builder: FlutterSmartDialog.init(),
    );
  }
}
